package com.nisco.nio.channel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class FileChannelTest2 {
    public static void main(String[] args)
        throws IOException {
        RandomAccessFile rf = new RandomAccessFile("E:/aa.txt", "rw");
        FileChannel channel = rf.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(10);
        int bytesRead = -1, i = 0;
        while ((bytesRead = channel.read(buffer)) != -1) {
            System.out.println("size = " + bytesRead);
            // 读取到当前位置
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print(buffer.get() + " ");
                i++;
            }
            System.out.println();
            buffer.clear();
        }
        rf.close();
        System.out.println(i);
    }
}
